package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.ShBanner;
import com.ruoyi.system.service.IShBannerService;
import com.ruoyi.system.utils.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/banner")
public class ShBannerController extends BaseController {
    @Autowired
    private IShBannerService shBannerService;

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("system:banner:list")
    @GetMapping("/list")
    public TableDataInfo list(ShBanner shBanner)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shBanner.setAppId(user.getAppIdm());
            }else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shBanner.setAppId(user.getAppIdf());
            }else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shBanner.setAppId(user.getAppId());
            }
        }
        startPage();
        List<ShBanner> list = shBannerService.selectShBannerList(shBanner);
        return getDataTable(list);
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("system:banner:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shBannerService.selectShBannerById(id));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("system:banner:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShBanner shBanner)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shBanner.setAppId(user.getAppIdm());
            } else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shBanner.setAppId(user.getAppIdf());
            } else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shBanner.setAppId(user.getAppId());
            }
        }
        return toAjax(shBannerService.insertShBanner(shBanner));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("system:banner:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShBanner shBanner)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            if (Base64Util.hasYwType(user.getYwType(), "1")) {
                shBanner.setAppId(user.getAppIdm());
            } else if (Base64Util.hasYwType(user.getYwType(), "2")) {
                shBanner.setAppId(user.getAppIdf());
            } else if (Base64Util.hasYwType(user.getYwType(), "3")) {
                shBanner.setAppId(user.getAppId());
            }
        }
        return toAjax(shBannerService.updateShBanner(shBanner));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("system:banner:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shBannerService.deleteShBannerByIds(ids));
    }
}
